"""
@File    :   models.py
@Time    :   2024/02/08 14:56:54
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   用户数据模型
"""

from datetime import datetime

from sqlalchemy import (
    JSON,
    BigInteger,
    Boolean,
    Column,
    DateTime,
    Index,
    Integer,
    String,
    UniqueConstraint,
)

from src.business.admin.system.user.enums import UserSexEnum
from src.config import get_app_settings
from src.enums.datetime import CONSTANT_NOT_DATETIME
from src.models.models import OrmBase

settings = get_app_settings()


class UserRoleRel(OrmBase):
    """User与Role多对多关系中间表的数据模型"""

    __tablename__ = "system_user_role_rel"
    __table_args__ = (UniqueConstraint("user_id", "role_id", "tenant_id"),)
    id = Column(BigInteger(), primary_key=True, autoincrement=True, comment="主键")
    # 租户ID(级联删除)
    tenant_id = Column(BigInteger(), nullable=False, index=True, comment="租户ID")
    # 用户ID(级联删除)
    user_id = Column(BigInteger(), nullable=False, index=True, comment="用户ID")
    # 角色ID(级联删除)
    role_id = Column(BigInteger(), nullable=False, index=True, comment="角色ID")


class User(OrmBase):
    """用户数据模型"""

    __tablename__ = "system_user"
    __table_args__ = (
        UniqueConstraint("username", "tenant_id"),
        # phone和email不用UniqueConstraint是因为它们的默认值是"", 在使用默认值时会导致联合唯一索引直接报IntegrityError
        Index(None, "id", "tenant_id"),
        Index(None, "phone", "tenant_id"),
        Index(None, "email", "tenant_id"),
    )

    id = Column(BigInteger(), primary_key=True, autoincrement=True, comment="主键")
    username = Column(String(20), nullable=False, comment="用户名")
    password_hash = Column(String(256), nullable=False, comment="密码hash")
    name = Column(String(50), default="", nullable=False, comment="使用人姓名")
    phone = Column(String(50), default="", nullable=False, comment="手机号码")
    email = Column(String(100), default="", nullable=False, comment="邮箱")
    sex = Column(
        String(20),
        nullable=False,
        default=UserSexEnum.unknown.value,
        comment="用户性别",
    )
    status = Column(
        Boolean(), default=True, nullable=False, comment="状态"
    )  # (True正常 False停用)
    is_admin = Column(
        Boolean(), default=False, nullable=False, comment="是否管理员用户"
    )
    create_datetime = Column(
        DateTime(), nullable=False, default=datetime.now, index=True, comment="创建时间"
    )
    # 创建者用户名(级联删除)
    creator_username = Column(
        String(20), default="", nullable=False, comment="创建者用户名"
    )
    # 修改者用户名(级联删除)
    updater_username = Column(
        String(20), default="", nullable=False, comment="修改者用户名"
    )
    remark = Column(String(300), default="", nullable=False, comment="备注")
    avatar = Column(
        String(300),
        default=settings.default_user_avatar,
        nullable=False,
        comment="头像",
    )
    can_login_ip_network = Column(
        JSON(), nullable=False, default=[], comment="允许登录的IP段列表"
    )
    number_of_simultaneous_logins = Column(
        Integer(), default=1, nullable=False, comment="可同时登录数量"
    )
    password_modification_deadline = Column(
        Integer(), default=90, nullable=False, comment="密码修改期限(天)"
    )
    # last_login_ip = Column(String(20), default="", nullable=False, comment="最后登录IP")
    last_login_datetime = Column(
        DateTime(),
        default=CONSTANT_NOT_DATETIME,
        nullable=False,
        index=True,
        comment="最后登录时间",
    )
    admin_last_modified_datetime = Column(
        DateTime(),
        default=CONSTANT_NOT_DATETIME,
        nullable=False,
        index=True,
        comment="管理员最后修改用户信息的时间",
    )
    admin_pwd_last_reset_datetime = Column(
        DateTime(),
        default=CONSTANT_NOT_DATETIME,
        nullable=False,
        index=True,
        comment="管理员最后重置密码的时间",
    )
    self_info_last_modified_datetime = Column(
        DateTime(),
        default=CONSTANT_NOT_DATETIME,
        nullable=False,
        index=True,
        comment="用户自身最后修改信息的时间",
    )
    self_psw_last_modified_datetime = Column(
        DateTime(),
        default=CONSTANT_NOT_DATETIME,
        nullable=False,
        index=True,
        comment="用户自身密码最后修改时间",
    )
    # 租户ID(级联删除)
    tenant_id = Column(BigInteger(), nullable=False, index=True, comment="租户ID")
